package com.ryan.sbnote.activity;

import java.util.HashMap;
import java.util.Map;

import android.content.ContentUris;
import android.content.ContentValues;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

import com.ryan.sbnote.R;
import com.ryan.sbnote.db.DbHelper;
import com.ryan.sbnote.db.DbHelper.Img;
import com.ryan.sbnote.db.DbHelper.Note;
import com.ryan.sbnote.util.Util;
import com.ryan.sbnote.view.NoteWriteView;
import com.ryan.sbnote.view.NoteWriteView.OnWriteChangedListener;
import com.ryan.util.BaseActivity;
import com.ryan.util.BitmapUtil;
import com.ryan.util.LogUtil;
import com.ryan.util.StringUtil;

public class NewNoteActivity extends BaseActivity {
	private static final String TAG = "NewNoteActivity";
	private NoteWriteView mEtWrite;
	private EditText mEtTitle;
	private EditText mEtText;

	private Map<String, Bitmap> mTextBufMap = new HashMap<String, Bitmap>();
	private int mTextBufFlag=0;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_note);
		mEtWrite = (NoteWriteView) findViewById(R.id.et_write);
		mEtTitle = (EditText) findViewById(R.id.et_title);
		mEtText = (EditText) findViewById(R.id.et_text);

		initFingerWrite();

		processBack();
		processSubmit(mSubmitClick);

	}

	
	private OnClickListener mSubmitClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			String title = mEtTitle.getText().toString();
			String text = mEtText.getText().toString();
			
			if(StringUtil.isEmpty(title)){
				shake(mEtTitle);
				return ;
			}
			
//			if(StringUtil.isEmpty(text)){
//				shake(mEtText);
//				return ;
//			}
			
			ContentValues values = new ContentValues();
			values.put(Note.USER, "");
			values.put(Note.TITLE, title);
			values.put(Note.DATA, text);
			values.put(Note.CREATE, System.currentTimeMillis());
			values.put(Note.VER, "");
			values.put(Note.SYN, 0);
			Uri insert = getContentResolver().insert(DbHelper.Note.CONTENT_URI, values);
			
			long noteId = ContentUris.parseId(insert);
			
			try {
				int idx;
				while((idx = text.indexOf(Util.NOTE_PREX))>=0){
					text = text.substring(idx+Util.NOTE_PREX.length());
					idx = text.indexOf(Util.NOTE_PREX_END);
					String flag = text.substring(0, idx);
					Bitmap bm = mTextBufMap.get(flag);
					if(bm!=null){
						byte[] bmByte = BitmapUtil.bitmap2Bytes(bm);
						ContentValues imgValues = new ContentValues();
						imgValues.put(Img.NOTE, noteId);
						imgValues.put(Img.KEY, flag);
						imgValues.put(Img.IMG, bmByte);
						getContentResolver().insert(DbHelper.Img.CONTENT_URI, imgValues );
					}
					text = text.substring(idx+Util.NOTE_PREX_END.length());
				}	
			} catch (Exception e) {
				LogUtil.e(TAG, "error at insert",e);
			}
			
			
			if(insert!=null){
				setResult(0);
			}
			finish();
		}
	};

	private void initFingerWrite() {
		double density = getResources().getDisplayMetrics().density;
		int len = 280;
		mEtWrite.init((int) (len * density), (int) (len * density));
		mEtWrite.setOnWriteChangedListener(new OnWriteChangedListener() {
			@Override
			public void writeChanged(Bitmap bitmap) {
				String flag = (mTextBufFlag++)+"";
				mTextBufMap.put(flag, bitmap);
				
				String key = Util.NOTE_PREX+flag+Util.NOTE_PREX_END;
				SpannableString textBuilder = new SpannableString(key);
				Drawable img = new BitmapDrawable(mTextBufMap.get(flag));
				img.setBounds(0, 0, img.getIntrinsicWidth(),
						img.getIntrinsicHeight());
				ImageSpan imgSpan = new ImageSpan(bitmap,
						ImageSpan.ALIGN_BASELINE);
				textBuilder.setSpan(imgSpan, 0, key.length(),
						Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				mEtText.getText().insert(mEtText.getSelectionStart(),
						textBuilder);
			}
		});
	}
}
